﻿using Abp.Extensions;
using Abp.UI;
using Mt.Site.Core.Organizations.UserCache.Models;
using Mt.Site.Core.WorkFlows.Design.Cache;
using Mt.Site.Core.WorkFlows.Enum;
using Mt.Site.Core.WorkFlows.Process;
using Mt.Site.Core.WorkFlows.Process.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Mt.Site.Core.WorkFlows.Diy.Actions.Impl
{
    public class SubmitAction : IAction
    {
        public string UniqueName => WorkFlowConst.OperationName.SubmitAction;

        public string JsActionName => "";

        public string JsOnSuccessActionName => "";

        public bool NeedCheckState => true;

        public async Task<StateWhenSubmit> CheckStateBeforeActionAsync(IWorkFlowManager workFlowManager, object param)
        {
            var paperState = workFlowManager.CurrentPaper.PaperState;
            var actionState = await workFlowManager.CheckActionStateAsync();

            bool needAddNewUsers = false;

            if (paperState == PaperState.Draft || (paperState == PaperState.Running && actionState == ActionState.Todo))
            {
                var current = await workFlowManager.GetCurrentProcessAsync();
                if (current.TrunkId == null)
                {
                    bool isHasOtherOperator = await workFlowManager.IsHasOtherOperatorAsync(current);

                    if (!isHasOtherOperator)
                    {
                        needAddNewUsers = true;
                    }
                }
            }

            //定制是否可以强制发往下一节点 及检查状态
            var result = await workFlowManager.WorkFlowNodePluginFunction.CheckDataBeforeSelelctUsersToNextNode(workFlowManager, needAddNewUsers);
            result.ReSetDefaultValue = true;
            return result;
        }

        public async Task<string> DisplayName(IWorkFlowManager manager)
        {
            await Task.FromResult(0);

            if (DiyStaticFunction.IsApprovalFlow(manager.WorkFlow.Definition.CustomData))
            {
                if (manager.IsFirst)
                {
                    return "提交";
                }
                return "通过审批";
            }
            else
            {
                var current = await manager.GetCurrentProcessAsync();
                if (current.CheckIsMainOperator())
                {
                    return "提交领导审批";
                }
                return "提交";
            }
        }

        public async Task<GetUserSelectionDataOutput> GetUserListAsync(IWorkFlowManager manager, Guid? nodeId, List<long> unitId)
        {
            if (nodeId != null)
            {
                var userFilter = manager.WorkFlow.FindNode(nodeId.Value).Definition.ToUserFilter();
                return await manager.GetUserListAsync(nodeId, unitId, userFilter, this.UniqueName);
            }
            else
            {
                return await manager.GetUserListAsync(nodeId, unitId, null, this.UniqueName);
            }
        }

        public async Task<bool> IsShowAsync(IWorkFlowManager manager)
        {
            var actionState = await manager.CheckActionStateAsync();
            var isBranchLine = await manager.IsBranchLineAsync();
            var isMultiplePeople = (actionState == ActionState.Todo ? await manager.IsMultiplePeopleAsync() : false);
            var hasNext = await manager.CanGoNextAsync();

            if (actionState == ActionState.Todo && (isBranchLine || isMultiplePeople || hasNext))
            {
                //var isShowBtn = await manager.WorkFlowNodePluginFunction.ShowSecificBtn(manager);
                //return isShowBtn;
                return true;
            }
            return false;
        }

        public bool NeedReply(IWorkFlowManager manager)
        {
            if (DiyStaticFunction.IsApprovalFlow(manager.WorkFlow.Definition.CustomData))
            {
                if (manager.IsFirst)
                {
                    return true;
                }
                return false;
            }
            else
            {
                return true;
            }
        }

        public bool NeedUserListArg => false;

        public async Task OperateAsync(IWorkFlowManager manager, object data)
        {
            var args = data as ActionArgs;
            if (args == null)
            {
                throw new UserFriendlyException("请选择人员及步骤");
            }

            var isBranchLine = await manager.IsBranchLineAsync();
            if (isBranchLine)
            {
                await manager.GoNextAsync(args.ReplyMsg);
            }
            else
            {
                if (DiyStaticFunction.IsApprovalFlow(manager.WorkFlow.Definition.CustomData))
                {
                    if (!manager.IsFirst)
                    {
                        if (args.ReplyMsg.IsNullOrEmpty())
                        {
                            args.ReplyMsg = "通过";
                        }
                    }
                }

                manager.AdjustAprovealResultStatus();
                await manager.GoNextAsync(args.NodeId, args.UserIdList, args.TurnRoundUserList, args.ActionScheme, args.ReplyMsg);
            }
        }
    }
}
